package com.desay.pangoo.itmt.controller;

import cn.hutool.core.util.StrUtil;
import com.desay.pangoo.common.lite.lang.web.BusinessException;
import com.desay.pangoo.common.lite.lang.web.CommonResponse;
import com.desay.pangoo.itmt.controller.vo.BUDivVo;
import com.desay.pangoo.itmt.controller.vo.contribution.ContributionDateRange;
import com.desay.pangoo.itmt.controller.vo.contribution.ContributionVo;
import com.desay.pangoo.itmt.controller.vo.contribution.req.ContributionReqVo;
import com.desay.pangoo.itmt.enums.ExceptionCodeEnum;
import com.desay.pangoo.itmt.service.ContributionLevelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * @author uids1175
 */

@RestController
@Api(tags = "ITMT 贡献度计算")
@RequestMapping(value = "/contribution")
@Validated
public class ContributionController {

    @Autowired
    private ContributionLevelService contributionLevelService;

    @GetMapping
    @ApiOperation(value = "获取指定时间范围内的贡献度 日期格式为 yyyy-MM", notes = "")
    public ResponseEntity<Object> getContribution(ContributionReqVo contributionVo) {
        if (!StrUtil.isAllNotEmpty(contributionVo.getBeginDate(),contributionVo.getEndDate())) {
            throw new BusinessException(ExceptionCodeEnum.DATE_FORMAT_ERROR);
        }
        ContributionVo contributionLevel = contributionLevelService.getContributionLevel(contributionVo);
        return new ResponseEntity<>(CommonResponse.success(contributionLevel), HttpStatus.OK);
    }


    @GetMapping("date/range")
    @ApiOperation(value = "获取目前贡献度支持检索的日期范围", notes = "")
    public ResponseEntity<Object> getContributionDateRange() {
        ContributionDateRange contributionDateRange = contributionLevelService.getContributionDateRange();
        return new ResponseEntity<>(CommonResponse.success(contributionDateRange), HttpStatus.OK);
    }

    @PostMapping("/{reviewId}")
    @ApiOperation(value = "根据指定的提案计算该提案的贡献度")
    public ResponseEntity<Object> addContributionByReview(@PathVariable("reviewId") Long reviewId) {
        if (reviewId == null) {
            throw new BusinessException(ExceptionCodeEnum.REQUEST_PARAMS_ERROR);
        }
        contributionLevelService.addContribution(reviewId);
        return new ResponseEntity<>(CommonResponse.success(),HttpStatus.OK);
    }
}
